﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace System.Reflection
{

    public static class ReflectionExtensions
    {

        public static TMetaAttribute GetCustomAttribute<TEnum, TMetaAttribute>(this TEnum self, Func<TEnum, TMetaAttribute> customMapping) where TMetaAttribute : Attribute
        {
            var customMappingResult = customMapping(self);
            if (customMappingResult != null)
            {
                return customMappingResult;
            }
            var r = self.GetCustomAttribute<TEnum, TMetaAttribute>();
            return r;
        }

        public static TMetaAttribute GetCustomAttribute<TEnum, TMetaAttribute>(this TEnum self, Func<TEnum, string> customMapping) where TMetaAttribute : Attribute
        {
            var customMappingResult = customMapping(self);
            if (customMappingResult != null)
            {
                return (TMetaAttribute)Activator.CreateInstance(typeof(TMetaAttribute), customMappingResult, null);
            }
            var r = self.GetCustomAttribute<TEnum, TMetaAttribute>();
            return r;
        }

    }

}
